在 Excel 的使用中,我们都会用到复制某一单元格数据,粘贴到多个单元格中的功能。对于我们来说这个功能是很方便的。这个功能和 Excel 中的拖拽功能类似(拖拽有数据单元格到其他单元格域)。
下面我们就介绍怎么用 Spread 实现以上两种该功能。
1.Spread 的拖拽功能。我们仅仅需要添加一行代码:
FpSPread1.AllowDragFill=true; 复制代码
2.Spread 实现复制某一单元格数据,粘贴到多个单元格中的功能:
在 Ctrl + C 赋值单元格之后,需要在 FpSpread1_ClipBoardPasting 事件中添加以下代码:
private void fpSpread1_ClipboardPasting(object sender, FarPoint.Win.Spread.ClipboardPastingEventArgs e)
{
FarPoint.Win.Spread.Model.CellRange cr = default(FarPoint.Win.Spread.Model.CellRange);
string textdata = null;
string[] a = null;
string[] b = null;
int rowcount = 0;
int colcount = 0;
cr = fpSpread1.Sheets[0].GetSelection(0);
if (cr.RowCount > 1 | cr.ColumnCount > 1)
{
e.Handled = true;
if (System.Windows.Forms.Clipboard.GetDataObject().GetDataPresent(System.Windows.Forms.DataFormats.Text))
{
textdata = (string)System.Windows.Forms.Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text);
a = textdata.Split(new char[] { (char)13 });
rowcount = a.Length - 1;
b = a[0].Split(new char[] { (char)9 });
colcount = b.Length;
for (int i = cr.Row; i <= cr.Row + cr.RowCount - 1; i += rowcount)
{
for (int x = 0; x <= rowcount - 1; x++)
{
b = a[x].Split(new char[] { (char)9 });
for (int j = cr.Column; j <= cr.Column + cr.ColumnCount - 1; j += colcount)
{
for (int y = 0; y <= colcount - 1; y++)
{
string myStr;
myStr = b[0];
fpSpread1.Sheets[0].SetValue(i + x, j + y, myStr.Trim((char)10, (char)30));
}
}
}
}
}
}
}
}
复制代码
Demo 下载:
编辑环境:Spread for WinForm 5.0 && VS 2010